<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class BerichtModel extends CI_Model {

	function __construct(){
		parent::__construct();
		$this->load->library('BerichtClass');
                $this->load->library('CommentaarClass');
	}

	private function getArrayFromRows($result){
		if($result->num_rows() > 0){
    		$return = array();
    		foreach($result->result() as $object){
    			$o = new BerichtClass();
    			$o->Id = $object->berichtid;
                        $commentaar = array();
                        $dbcommentaar = $this->SelectCommentaar($object->berichtid);
                        foreach($dbcommentaar->result() as $c){
                            $co = new CommentaarClass();
                            $co->Id = $c->berichtcommentaarid;
                            $co->BerichtId = $c->bericht;
                            
                            $co->SpelerId = $c->spelerid;
                            if($co->SpelerId == null){
                                $co->SpelerNaam = 'ADMIN';
                            } else {
                                $co->SpelerNaam = $c->naam.' '.$c->voornaam;
                            }
                                        
                           
                            $co->Commentaar = $c->commentaar;
                            $co->Tijdstip = $c->tijdstip;
                            $commentaar[] = $co;
                        }
                        $o->Titel = $object->titel;
                        $o->Bericht = $object->bericht;
                        $o->Tijdstip = $object->tijdstip;
                        $o->Commentaar = $commentaar;
                       
    			$return[] = $o;
    		}

    		return $return;
    	} else {
    		return null;
    	}
	}


	
    function SelectAll(){
    	$query = "select * from bericht where deleted = 0 order by tijdstip desc";
    	$result = $this->db->query($query);
    	return $this->getArrayFromRows($result);
    	
    }
    
    function SelectTop($i){
        $query = "select * from bericht where deleted = 0 order by tijdstip desc limit ?";
    	$result = $this->db->query($query,$i);
    	return $this->getArrayFromRows($result);
    }
    
    function SelectCommentaar($id){
        $query = "select * from berichtcommentaar left join speler on speler.spelerid = berichtcommentaar.schrijver where berichtcommentaar.bericht = ? and (berichtcommentaar.deleted = 0) order by tijdstip desc";
        return $this->db->query($query,array($id));
    }

    function SelectById($id){
    	$query = "select * from bericht where berichtid = ? and deleted = 0 order by tijdstip desc";
    	$result = $this->db->query($query,array($id));
    	return $this->getArrayFromRows($result);
    	
    }
    
    function InsertBericht($titel,$bericht,$type){
        $query = "insert into bericht (titel,bericht,type) values (?,?,?)";
        $result =  $this->db->query($query,array($titel,$bericht,$type));
        return $this->db->insert_id();
    }
    
    function InsertCommentaar($bericht,$commentaar,$speler = null){
        if($speler == null){
            $query = "insert into berichtcommentaar (bericht, commentaar) values (?,?)";
            $this->db->query($query,array($bericht,$commentaar));
        }
        else{
            $query = "insert into berichtcommentaar (bericht, commentaar, schrijver) values (?,?,?)";
            $this->db->query($query,array($bericht,$commentaar,$speler));
        }
        //die($query);
        return $this->db->insert_id();
    }
    
    function UpdateBericht($id,$bericht,$titel){
        $query = "update bericht set bericht = ?, titel = ? where berichtid = ?";
        return $this->db->query($query,array($bericht,$titel,$id));
    }
    
    function DeleteCommentaar($id){
        $query = "update berichtcommentaar set deleted = 1 where berichtcommentaarid = ?";
        return $this->db->query($query,array($id));
    }
    
    function DeleteBericht($id){
        $query = "update bericht set deleted = 1 where berichtid = ?";
        return $this->db->query($query,array($id));
    }

   
    
    
	
}
